package com.dongdongsongcan.app.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RadioGroup;

import com.dongdongsongcan.app.R;
import com.dongdongsongcan.app.activity.OrderDetailActivtiy;
import com.dongdongsongcan.app.adapter.OrderListAdapter;
import com.dongdongsongcan.app.common.DDMessageDef;
import com.dongdongsongcan.app.control.OrderControl;
import com.dongdongsongcan.app.control.bean.OrderListData;
import com.dongdongsongcan.app.lib.pulltorefresh.PullToRefreshBase;
import com.dongdongsongcan.app.lib.pulltorefresh.PullToRefreshListView;
import com.su.dolphin.control.task.CtrlTask;
import com.su.dolphin.core.AppThread;
import com.su.dolphin.core.notification.DpMessageCenter;
import com.su.dolphin.utils.ui.TipsUtil;
import com.su.dolphin.view.fragment.BaseFragment;

public class OrderListFragment extends BaseFragment implements DpMessageCenter.INotify {

    private PullToRefreshListView list_orders_list;
    private OrderListAdapter mOrderListAdapter;
    private String mDate;
    private RadioGroup mOrder_tabs;
    private int mOffset;
    private int PAGE_SIZE = 10;
    private Dialog mProgressDialog;

    @Override
    protected int onSetContentView() {
        DpMessageCenter.i().register(this, DDMessageDef.N_SUBMIT_ORDER_SUCCESS);
        return R.layout.fragment_orderlist;
    }

    @Override
    public void initView() {
        mProgressDialog = TipsUtil.buildDPProgressDialog(getActivity(), "载入中");
        mOrder_tabs = (RadioGroup) getView().findViewById(R.id.order_tabs);
        list_orders_list = (PullToRefreshListView) getView().findViewById(R.id.list_orders_list);
        mOrderListAdapter = new OrderListAdapter(getActivity());
        list_orders_list.setAdapter(mOrderListAdapter);
        list_orders_list.getRefreshableView().setEmptyView(getView().findViewById(R.id.order_list_emptyView));
        list_orders_list.setMode(PullToRefreshBase.Mode.BOTH);

    }

    @Override
    public void setListener() {
        mOrder_tabs.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.order_tab1:
                        mDate = "today";
                        break;
                    case R.id.order_tab2:
                        mDate = "month";
                        break;
                    case R.id.order_tab3:
                        mDate = "previous";
                        break;
                }
                refreshOrders();
            }
        });
        list_orders_list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                refreshOrders();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                getOrders();
            }
        });
        list_orders_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                OrderDetailActivtiy.startFrom(getActivity(), mOrderListAdapter.getItem(position - 1).getOrder_id());
            }
        });
    }

    private void refreshOrders() {
        mOffset = 0;
        mOrderListAdapter.clear();
        getOrders();
    }

    @Override
    public void initData() {
        mProgressDialog.show();
        mDate = "today";
        refreshOrders();
    }

    public void getOrders() {
        OrderControl.getOrders(mDate, mOffset, PAGE_SIZE, new CtrlTask.IControlResponse<OrderListData>() {
            @Override
            public void onControlResponse(OrderListData data) {
                mProgressDialog.dismiss();
                if (data.getError() == 0) {
                    mOffset = data.getOffset();
                    mOrderListAdapter.setData(data.getResult());
                }
                list_orders_list.onRefreshComplete();
            }
        });
    }

    @Override
    public void onClick(View arg0) {

    }

    @Override
    public void update(Bundle bundle) {

    }

    @Override
    public void onNotifyInner(DpMessageCenter.MCMessage mcmessage) {
        if (mcmessage.notificationID == DDMessageDef.N_SUBMIT_ORDER_SUCCESS) {
            AppThread.uiPost(new Runnable() {
                @Override
                public void run() {

                }
            },8);
            initData();
        }
    }
}
